# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: fossdd <fossdd@pwned.life>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=mautrix-whatsapp
pkgver=0.11.0
pkgrel=0
pkgdesc="Matrix-WhatsApp puppeting bridge"
url="https://maunium.net/go/mautrix-whatsapp"
arch="all"
license="AGPL-3.0-or-later"
makedepends="go olm-dev sqlite-dev"
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-upgrade"
subpackages="$pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/mautrix/whatsapp/archive/v$pkgver.tar.gz
	mautrix-whatsapp.initd
	mautrix-whatsapp.confd
	"
builddir="$srcdir/whatsapp-$pkgver"
options="net" # downloading go modules

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	export CGO_CFLAGS="$CFLAGS"
	export CGO_LDFLAGS="$LDFLAGS"
	local _goldflags="
		-X main.Tag=$pkgver
		-X 'main.BuildTime=$(date -d @"$SOURCE_DATE_EPOCH" '+%b %_d %Y, %H:%M:%S')'
		"

	go build -ldflags "$_goldflags" ./cmd/mautrix-whatsapp

	# write example config.yaml for installing in package()
	./mautrix-whatsapp -e
}

check() {
	go test -v ./...
}

package() {
	install -Dm755 mautrix-whatsapp \
		-t "$pkgdir"/usr/bin/
	install -Dm644 config.yaml \
		-t "$pkgdir"/etc/mautrix-whatsapp/

	install -Dm755 "$srcdir"/mautrix-whatsapp.initd \
		"$pkgdir"/etc/init.d/mautrix-whatsapp
	install -Dm644 "$srcdir"/mautrix-whatsapp.confd \
		"$pkgdir"/etc/conf.d/mautrix-whatsapp
}

sha512sums="
aea64591a0e1355ef379797e4b912e507536e09d02c2574b6a1f9a533b5f22f47c90b11f295eddbcddc23e7242efaaf7affc130d1345ea42f2d1b52e627b9b95  mautrix-whatsapp-0.11.0.tar.gz
320ec426f033e93297bb3dd2ebe6996a9a677c53e76e8eb6d4b6f2bb24c1c756ef8d38d2dbb0d038369507fd9bf4864e73ab86783be3f6bbca150fe46a669841  mautrix-whatsapp.initd
9349b660273c63d2973f1b99ddbd98469dddc098157380603210159f17d3cb1eb55e71dbd21550b20d40831f4da320225e7c03441667e2750e30a2e1fa03acfe  mautrix-whatsapp.confd
"
